// 导入数据库操作
const { sqlOne, sqlBatch } = require('../db/common')
// 加密工具包bcryptjs
const bcrypt = require('bcryptjs')
// 引入jwt
const jwt = require('jsonwebtoken')
// 引入加密字符串
const config = require('../utils/config')
// 导入工具
const utils = require('../utils/index')
const trie = require('../utils/trie');
// 成功响应 onSuccess 失败响应 onError

// 获取文章下的评论
exports.get = async (req, res) => {
    const user_id = req.auth.id
    const id = req.query.id
    console.log(user_id,id);
    // 查询用户对于谋篇文章的点赞和收藏情况
    const sql1 = `select * from t_article_like where user_id = ? and article_id = ?;`;
    const sql2 = `select * from t_article_collect where user_id = ? and article_id = ?;`;
    const [results1, results2] = await sqlBatch(sql1 + sql2, [user_id, id, user_id, id])
    res.onSuccess({ like: results1.length != 0, collect: results2.length != 0 })
}


exports.collect = async (req, res) => {
    
    const user_id = req.auth.id
    const id = req.query.id
    const sql1 = `select id from t_article_collect where user_id = ? and article_id = ?;`;
    const results1 = await sqlOne(sql1, [user_id, id])
    if(results1.length>0){
        // 删除
        const sql2 = `delete from t_article_collect where id = ?`
        await sqlOne(sql2, [results1[0].id])
        return res.onSuccess(null,'取消成功')
    }
    const sql = `insert into t_article_collect (user_id,article_id) values (?,?)`
    await sqlOne(sql, [user_id, id])
    res.onSuccess(null,"收藏成功")
}
exports.like = async (req, res) => {
    const  user_id = req.auth.id
    const  id  = req.query.id
    const sql1 = `select id from t_article_like where user_id = ? and article_id = ?;`;
    const results1 = await sqlOne(sql1, [user_id, id])
    if(results1.length>0){
        // 删除
        const sql2 = `delete from t_article_like where id = ?`
        await sqlOne(sql2, [results1[0].id])
        return res.onSuccess(null,'取消成功')
    }
    const sql = `insert into t_article_like (user_id,article_id) values (?,?)`
    await sqlOne(sql, [user_id, id])
    res.onSuccess(null,'点赞成功')
}